﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TodayWhoModel
{
    public class VoteModel
    {
        #region Fields
        private int vid;

        public int Vid
        {
            get { return vid; }
            set { vid = value; }
        }
        private int profileUserId;

        public int ProfileUserId
        {
            get { return profileUserId; }
            set { profileUserId = value; }
        }
        private int voterUserId;

        public int VoterUserId
        {
            get { return voterUserId; }
            set { voterUserId = value; }
        }
        private DateTime date;

        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }
        private bool type;

        public bool Type
        {
            get { return type; }
            set { type = value; }
        }

        #endregion

        public bool Insert(string userName)
        {
            string ssql = "INSERT INTO [Votes] (profileUserId,voterUserId,type) ";
            ssql += " (SELECT userId,@voterUserId,@type FROM [Users] WHERE userName=@userName)";
            
            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userName", userName);
            cmd.Parameters.AddWithValue("@voterUserId", voterUserId);
            cmd.Parameters.AddWithValue("@type", Type);

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        public int CountPlusVotes(string userName)
        {
            string ssql = "SELECT COUNT(*) FROM [Votes] WHERE profileUserId=(SELECT userId FROM [Users] WHERE userName=@userName) AND type=1";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userName", userName);

            return DatabaseHelper.ExecuteScalar(cmd);
        }

        public int CountMinusVotes(string userName)
        {
            string ssql = "SELECT COUNT(*) FROM [Votes] WHERE profileUserId=(SELECT userId FROM [Users] WHERE userName=@userName) AND type=0";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userName", userName);

            return DatabaseHelper.ExecuteScalar(cmd);
        }
    }
}
